package com.gel.codegeneration.core;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.gel.codegeneration.ai.model.entity.HtmlCodeResult;
import com.gel.codegeneration.ai.model.entity.MultiFileResult;
import com.gel.codegeneration.ai.model.enums.CodeGenTypeEnum;
import dev.langchain4j.model.output.structured.Description;

import java.io.File;

/**
 * @BelongsProject: code-generation-platform
 * @BelongsPackage: com.gel.codegeneration.core
 * @Author: gel
 * @CreateTime: 2025-08-22  11:19
 * @Description: TODO
 * @Version: 1.0
 */
@Deprecated
public class CodeFileSaver {
    //文件保存根目录
    private static final String ROOT_PATH = System.getProperty("user.dir") + "/temp/code_output";

    /**
     * @description: 保存Html文件结果
     * @author: gel
     * @param: HtmlCodeResult
     * @return: java.io.File
     **/
    public static File saveHtmlCodeResult(HtmlCodeResult htmlCodeResult) {
        String uniquePath = buildUniquePath(CodeGenTypeEnum.HTML.getValue());
        writeSingleFile(uniquePath, "index.html", htmlCodeResult.getHtmlCode());
        return new File(uniquePath);

    }
    /**
     * @description: 多存MultiFileResult结果
     * @author: gel
     * @param: [bizType]
     * @return: java.lang.String
     **/
    public static File saveMultiFileResult(MultiFileResult multiFileResult) {
        String uniquePath = buildUniquePath(CodeGenTypeEnum.MULTI_FILE.getValue());
        writeSingleFile(uniquePath, "index.html", multiFileResult.getHtmlCode());
        writeSingleFile(uniquePath, "style.css", multiFileResult.getCssCode());
        writeSingleFile(uniquePath, "script.js", multiFileResult.getJsCode());
        return new File(uniquePath);
    }


    //构建唯一目录路径
    private  static String buildUniquePath(String bizType) {
        String uniqueDirName=String.format("%s_%s",bizType, IdUtil.getSnowflakeNextId());
        String uniquePath = ROOT_PATH + File.separator + uniqueDirName;
        FileUtil.mkdir(uniquePath);
        return uniquePath;
    }

    //单个文件写入
    public static void writeSingleFile(String dirPath, String fileName, String content) {
        String path = dirPath + File.separator + fileName;
        FileUtil.writeUtf8String(content, path);
    }
}
